<?php
  require_once('../db.php');
  require_once('../header.php');


  pr($_GET);
  // searching Start
  if(isset($_GET['search']) && !empty($_GET['search'])) {
      $search = $_GET['search'];
      $where = "WHERE `uname` LIKE '%$search%'";
  } else {
    $search = '';
    $where = '';
  }
  // search end

  // Sorting Start
    $field = isset($_GET['field']) && !empty($_GET['field']) ? $_GET['field'] : 'added_date';
    $order = isset($_GET['order']) && !empty($_GET['order']) ? $_GET['order'] : 'asc';
    $newOrder = $order=='asc'?'desc':'asc';
    $orderBy = "ORDER BY `$field` $order";
  // Sorting end


  if(isset($_GET['mode']) &&
   $_GET['mode'] =='status'){

   $id = $_GET['id'];
   $currentStatus  = $_GET['cstatus'];
   $newStatus = $currentStatus==1?0:1;

   $updateStatusQuery = "UPDATE `users` SET `status`='$newStatus' WHERE `id`='$id'";
    pq($updateStatusQuery);

    mysql_query($updateStatusQuery) or die(mysql_error());
    if(mysql_affected_rows()==1){
      echo "User's status updated successfully.";
    }

  }

  ## DELETE USER
  if(isset($_GET['mode']) &&
   $_GET['mode'] =='del'){

    $id = $_GET['id'];
    $delQuery = "DELETE FROM `users` WHERE `id`='$id'";

    pq($delQuery);

    mysql_query($delQuery) or die(mysql_error());
    if(mysql_affected_rows()==1){
      echo "User deleted successfully.";
    }
  }
  ## END DELETE ##


  # GEt total users
  $countUsersQuery = "SELECT COUNT(*) as `total` FROM `users` $where";

  pq($countUsersQuery);
  $countUsersResult = mysql_query($countUsersQuery) or die(mysql_error());
  $totalUsersAns = mysql_fetch_assoc($countUsersResult);
  pr($totalUsersAns);
  $totalusers = $totalUsersAns['total'];
  $rpp = 5;
  $numberOfPages = ceil($totalusers / $rpp);
  $currentPage = isset($_GET['page']) && !empty($_GET['page'])?$_GET['page']:1;
  $index = ($currentPage - 1) * $rpp;
  $limit = "LIMIT $index,$rpp";
  ##############################

  $usersQuery = "SELECT * FROM `users` {$where} {$orderBy} {$limit}";
//var_dump($orderBy);exit;
  pq($usersQuery);

  $usersResult = mysql_query($usersQuery) or die(mysql_error());

?>
<!--- Search Form --->
<form action="" method="get">
  <input type="search" name="search" placeholder="Search Here" />
  <input type="submit" value="Search" />
</form>
<hr>
<!-----Export users----->
<a href="<?=SITE_PATH?>/export.php?search=<?=$search?>">Export Users</a>
/
<!-----Import users----->
<a href="<?=SITE_PATH?>/import.php">Import Users</a>
<hr>
<table  border="1" cellpadding="0" cellspacing="0" width="80%" align=
"center">
  <thead>
    <tr>
      <th colspan="6">:: All Users ::<em>(<?=$totalusers?>)</em> users found.</th>
    </tr>
    <tr>
      <th>#</th>
      <th>
<a href="index.php?field=uname&order=<?=$newOrder?>&search=<?=$search?>">
        Name
</a>
      </th>
      <th>Email</th>
      <th>Added Date</th>
      <th>Avatar</th>
      <th>Status</th>
      <th>Action</th>
    </tr>
  </thead>
  <tbody>
<?php
  while($user = mysql_fetch_assoc($usersResult))
  {
    //pr($user);
    $status = $user['status'];
    $id = $user['id'];
    $email = $user['email'];
?>

    <tr>
      <td><?=$id?></td>
      <td><?=$user['uname']?></td>
      <td><a href="mailto:<?=$email?>"><?=$email?></a></td>
      <td><?=date('d-M-Y G:i a',strtotime($user['added_date']))?></td>

      <td>
<a target="_blank" href="../uploads/<?=$user['avatar']?>">
<img alt="Image" title="User's Image" src="../uploads/thumb/<?=$user['avatar']?>" />
</a>
      </td>

      <td><a href="?mode=status&cstatus=<?=$status?>&id=<?=$id?>"><?=getStatus($status)?></a></td>
      <td><a target="_blank" href="update_user.php?id=<?=$id?>">Edit</a> / <a href="?mode=del&id=<?=$id?>" onClick="return confirm('Are you sure ?')">Delete</a></td>
    </tr>
<?php
  }
?>


  </tbody>
</table>
<!------- Paging ------>
<ul class="paging">
<?php
for($i=1 ; $i<=$numberOfPages ; $i++)
{
  if($currentPage == $i) {
    $class = 'class="active"';
    $pageNum = $i;
  } else {
    $class = '';
    $pageNum = "<a href=\"index.php?page=$i&search=$search\">$i</a>";
  }
?>
  <li <?=$class?>><?=$pageNum?></li>
<?php
}//end for
?>
</ul>
<style type="text/css">
  .paging {
    /*background-color: #FFFF00;*/
    float: left;
    list-style: none outside none;
    margin-left: 60px;
    padding-left: 0;
    width: 100%;
  }
  .paging li {
    background-color: #FFC0CB;
    border: 1px solid #FF0000;
    float: left;
    height: 30px;
    text-align: center;
    width: 10%;
  }
  li.active {
    font-size:20px;
    color:red;
  }
</style>
<!----------------------------------->
<hr>
Debug :
<hr>
<?php

foreach ($arrLog as $log)
{
  echo $log;
}
